cap log close
log using ${log}\11_yearly_panel.log, replace


/*
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	
	SIAB Preparation
	
	Transfer data set into yearly panel (using one specific cutoff date)

	
	Author(s): Wolfgang Dauth, Johann Eppelsheimer

	Version: 1.0
	Created: 2018-06-01
	
	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

*/



/********************************************************************************
* Set cutoff date
********************************************************************************
global cutoffMonth	06		// cutoff month
global cutoffDay 	30		// cutoff day


/*
	Example:
	
	To set the cutoff date to June 30 assign 
		06 to cutoffMonth and 
		30 to cutoffDay.
*/


********************************************************************************
* Transfer dates into strictly ascending numbers
********************************************************************************

* cutoff date
gen cutoff_num = 100 * $cutoffMonth + $cutoffDay

* begin of episodes
gen begepi_num = 100 * month(begepi) + day(begepi)

* end of episodes
gen endepi_num = 100 * month(endepi) + day(endepi)


********************************************************************************
* Flag episodes that include the cutoff date
********************************************************************************
gen cutoff_incl = 0
replace cutoff_incl = 1 if begepi_num <= cutoff_num & cutoff_num <= endepi_num

sum cutoff_incl


********************************************************************************
* Keep only episodes that include the cutoff date
********************************************************************************
keep if cutoff_incl == 1


********************************************************************************
* Clean up
********************************************************************************
drop cutoff_num begepi_num endepi_num cutoff_incl

*/


// I NEED TO CREATE A DIFFERENT VERSION as the cutoff rule might exclude spells not falling in the cutoff 

cap drop x
cap drop longspell 

bys persnr jahr: egen x = max(dauer)
bys persnr jahr: gen longspell = 1 if dauer == x

sort persnr begepi 
jahr quelle longspell
br persnr jahr quelle dauer longspell

tab quelle longspell


* GENERATE A VARIABLE THAT INDICATES THE QUELLE OF THE VARY LAST SPEL 
	
	cap drop x 
	
	bys persnr (endepi): gen x = quelle if _n == _N 
	bys persnr (endepi): egen lastquelle = max(x)
	
	cap drop x 
	

* Generate the variables from the last employment so that the info is preseent also if the longest last spell is unemployment

cap drop x_* last_*
foreach var in wage_imp_fte totempdays totempdays_nomarginal meanempdays meanempdays_nomarginal annualearn_nomarginal annualearn meandailywage_nomarginal meandailywage annualearn_nomarginal_fte annualearn_fte az_d alter_mw az_vz az_f_vz az_ges wz08_kons_num {
	
	bys persnr emp (begepi): gen x_`var' = `var' if _n == _N & emp == 1
	bys persnr (begepi): carryforward x_`var', gen(last_`var') 
	
}


* Generate variable containing the beg and end of the last employment spell for each individual (even if it is not the longest spell in the last year)

cap drop x y

	bys persnr emp (begepi): gen x = begepi if _n == _N & emp == 1
	bys persnr emp (begepi): gen y = endepi if _n == _N & emp == 1

	bys persnr: egen beglastemp = max(x)
	bys persnr: egen endlastemp = max(y)

		cap drop x y 

foreach var in wage_imp_fte totempdays totempdays_nomarginal meanempdays meanempdays_nomarginal annualearn_nomarginal annualearn meandailywage_nomarginal meandailywage annualearn_nomarginal_fte annualearn_fte az_d alter_mw az_vz az_f_vz az_ges wz08_kons_num  {
	
	replace last_`var' = `var' if last_`var' == .
	
}



* MAX NUMBER OF ELIGIBLE AND NON ELIGIBLE PEOPLE FOR EACH INDIVIDUAL AND YEAR 

		local varlist *ita *span *port
		
		foreach var of varlist `varlist' {
			
			bys persnr jahr: egen max_`var'_fy = max(`var'_fy) 	// _fy is suffix for firm-year
			
		}

		
		

*gsort persnr jahr -begepi
*by persnr jahr: keep if _n == 1 // I keep the last spell in each year
	
	// Other option. I keep the employment spells as priority and within employment spells the longest, otherwise the unemp spells
	
keep if longspell == 1

log close



